package org.example.utils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 李国树
 * @version 1.0
 * @Desc
 * @date 2023/11/29 14:23
 */
public class DateDiffUtils {
    /**
     * 求2个日期相差的月份
     * @param date1
     * @param date2
     * @return
     */
    public static int getDiffMonth(Date date1, Date date2) throws ParseException {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        int year1 = calendar1.get(Calendar.YEAR);
        int month1 = calendar1.get(Calendar.MONTH);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        int year2 = calendar2.get(Calendar.YEAR);
        int month2 = calendar2.get(Calendar.MONTH);
        int diffYear = year2 - year1;
        int diffMonth = month2 - month1;
        int diff = diffYear * 12 + diffMonth;
        return diff;
    }
}

 
